<template>
  <el-dialog v-model="dialogFormVisible" :title="title" width="800">
    <el-form :model="form" label-width="auto" style="max-width: 800px" :rules="rules">
      <div class="form-scroll">
        <el-row :gutter="20">
          <template v-for="(item, index) in Object.keys(form)">
            <template v-if="item !== 'list' && form[item].type !== 'textarea'">
              <el-col :span="12">
                <el-form-item :label="item" :prop="item">
                  <template v-if="form[item].type === 'datetime'">
                    <el-date-picker
                      v-model="form[item].value"
                      type="date"
                      :placeholder="`请选择${item}`"
                      value-format="YYYY-MM-DD"
                    />
                  </template>
                  <template v-else-if="form[item].type === 'select'">
                    <template v-if="item === '任务地点'">
                      <el-select v-model="form[item].value" placeholder="请选择任务地点">
                        <el-option label="渝北区龙兴镇-饮水库" value="渝北区龙兴镇-饮水库"></el-option>
                        <el-option label="渝北区木耳镇-污水厂" value="渝北区木耳镇-污水厂"></el-option>
                      </el-select>
                    </template>
                  </template>
                  <template v-else>
                    <el-input v-model="form[item].value" :placeholder="`请填写${item}`" />
                  </template>
                </el-form-item>
              </el-col>
            </template>
            <template v-else>
              <template v-if="form[item].type === 'textarea'">
                <el-col :span="24">
                  <el-form-item :label="item" :prop="item">
                    <el-input type="textarea" v-model="form[item].value" :placeholder="`请填写${item}`" />
                  </el-form-item>
                </el-col>
              </template>
            </template>
          </template>
        </el-row>
      </div>
    </el-form>
    <div>
      <el-button type="primary" @click="onSubmit">{{ isEdit? '编辑': '新增' }}</el-button>
      <el-button @click="dialogFormVisible = false">取消</el-button>
    </div>
  </el-dialog>
</template>

<script setup>
  // 方法
  import { ref, defineExpose } from 'vue'
  const instance = getCurrentInstance();
  const { proxy } = instance || {};

  // 显示表单
  const dialogFormVisible = ref(false)
  const title = ref('新增耗材')
  const isEdit = ref(false)
  // 表单
  const form = ref({
    "耗材名称": { "type": "", "value": "" },
    "描述": { "type": "textarea", "value": "" },
    "出入库记录": { "type": "textarea", "value": "" },
    "类型": { "type": "", "value": "" },
    "规格": { "type": "", "value": "" },
    "状态": { "type": "", "value": "" },
    "库存数量": { "type": "", "value": "" },
    "预警值": { "type": "", "value": "" },
    "单位": { "type": "", "value": "" },
    "负责人": { "type": "", "value": "" }
  })
  // 验证
  const rules = ref({})

  // 打开方法
  const open = () => {
    for(let i in form.value) {
      form.value[i].value = ''
    }
    dialogFormVisible.value = true
    isEdit.value = false
    title.value = '新增耗材'
  }
  // 编辑
  const edit = (data) => {
    for(let i in form.value) {
      form.value[i].value = data[i]
    }
    isEdit.value = true
    dialogFormVisible.value = true
    title.value = '编辑耗材'
  }
  // 关闭方法
  const close = () => {
    dialogFormVisible.value = false
  }

  // 提交
  const onSubmit = () => {
    for(let i in form.value) {
      if(!form.value[i].value) {
        if(form.value[i].type === 'select' || form.value[i].type === 'datetime') {
          proxy.$modal.msgWarning('请选择'+i);
        }else {
          proxy.$modal.msgWarning('请t填写'+i);
        }
        return
      }
    }
    proxy.$modal.msgSuccess(title.value + '成功');
    close()
  }

  defineExpose({
    open, close, edit
  })

</script>

<style scoped lang="scss">

</style>
